/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package universityModel.organization;

import universityModel.person.Student;
import universityModel.utils.ArrayModel;

/**
 *
 * @author Дмитрий
 */
/**
 * Describe a one course
 */
public class Course {
    private int level;
    private ArrayModel<ExtendedGroupView> groups = new ArrayModel<ExtendedGroupView>();
        
    public Course(int level) {
	this.level = level;
    }
		
    public void addGroup(String name) {
    	if(!isGroup(name)) {
            Group newGroup = new Group(name, level);
            groups.add(newGroup);
    	}
    }
    
    public int getLevel() {
	return level;
    }
	
    public void delGroup(String name) {
        groups.remove(findGroupIndex(name));
    }
    
    public void addStudent(Student student) {
	getGroup(student.getNameGroup()).addStudent(student);
    }
	
    public void delStudent(Student student){
            getGroup(student.getNameGroup()).delStudent(student);
    }
	
    public boolean isGroup(String name) {
        return findGroupIndex(name) != -1; 
    }
	
    /**
    *For print operations
    */
    public ArrayModel<ExtendedGroupView> getGroups() {
	return new ArrayModel<ExtendedGroupView>(groups);
    }

    public ExtendedGroupView getGroup(String name) {
	int index = findGroupIndex(name);
	return index != -1 ? groups.get(index) : new Group(name, level);  
    }
		
    /**
    * Searching a group index
    * @param name is a group that we want to find
    * @return index at collection;
    */
    private int findGroupIndex(String group) {
	for(int index = 0; index < groups.size(); index++) {
            if(groups.get(index).getName().equals(group)) {
                return index;
            }
	}
	return -1;
    }
    
    
//    public void ShowCourse() {
//        for(int index = 0; index < groups.size(); index++) {
//            groups.get(index).showAllStudents();
//        }
//    }
}
